<?php
namespace app\common\controller;
use think\Loader;
use think\Controller;
use think\Db;

/**
* 后台公用基础控制器
* @author taigu
* @version 2017-9-22
*/
class IdexBaseController extends Controller {
	
    protected function _initialize() {
        parent::_initialize();
        
        if (!session('info_user')) {
            $this->redirect('index/index/login');
        }
        $this->assign('action',request()->action());
    }
	
	//图片上传
    public function upload(){
    	include_once ('././vendor/aliyun-oss-php-sdk/autoload.php');
		$files = $_POST["value"];  
	    $tmp = base64_decode($files);
	    $fn = time()."_".rand(100000, 999999) . ".jpg";
		$fp = '././public/uploads/image/'. $fn;
		$fp1 = '././public/uploads/image/'. $fn;
	    $res=file_put_contents( $fp, $tmp);
		$ossClient = new \OSS\OssClient('LTAIaqGtJ3JDXJvv', 'Q2kTcyepAHX0VOR44OtszVoyOKE8gl', 'oss-cn-shanghai.aliyuncs.com');
		try{
			$res=$ossClient->uploadFile("long3",$fn,$fp1);
			$src='http://long3.oss-cn-shanghai.aliyuncs.com/'.$fn;
			unlink($fp1);
			return json(["code"=>200,'url'=>$src]);
		} catch(OssException $e) {
			return json(["code"=>404,'msg'=>'上传错误']);
		} 
	} 
	
}
